﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;
using System.Web.Mvc;

namespace Nikola.Filter
{
    public class ActionFilter : System.Web.Mvc.ActionFilterAttribute
    {
        /// <summary>
        /// Action之后执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }

        /// <summary>
        /// Action之前执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext != null)
            {
                string currentAction = filterContext.ActionDescriptor.ActionName;
                string currentControl = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                string controlAction = currentControl + "/" + currentAction;

                //取出Cookice
                //var userInfo = CommonHelper.SerializableByCookie<UserInfo>("Token");

                //if (userInfo == null)
                //    HttpContext.Current.Response.Redirect(@"/UserInfo/Index");
                //else
                //{
                //    bool isPass = false;
                //    //判断当前action是否有权限
                //    var allAction = userInfo.Actions.Where(a => a.ActionType == 1);
                //    if (allAction.Any(ac => ac.ActionURL == controlAction))
                //    {
                //        dynamic v = filterContext.Controller.ViewBag;
                //        v.Menus = userInfo.Actions.Where(a => a.ActionType == 1);
                //        v.Roles = string.Join(",", userInfo.Roles);
                //        v.UserName = userInfo.RealName;
                //        isPass = true;
                //    }
                //    if (!isPass)
                //    {
                //        if (currentAction == "Index")
                //            HttpContext.Current.Response.Redirect(@"/UserInfo/Index");
                //        filterContext.Controller.ViewBag.Menus = userInfo.Actions.Where(a => a.ActionType == 1);
                //    }
                //}

                //RecordHistory(userInfo.UserID, filterContext, currentAction, currentControl);
            }
            base.OnActionExecuting(filterContext);
        }
        

        /// <summary>
        /// Result之前执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {

        }

        /// <summary>
        /// Result之后执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {

        }
    }
}